package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.EmployeeBean;
import com.project.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("em")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @RequestMapping("login")
    public EmployeeBean login(String name, String pwd, HttpServletRequest req) {
        EmployeeBean em = service.login(name, pwd);
        req.getSession().setAttribute("loginEm", em);
        req.getSession().setAttribute("emList", new HashMap<Integer, EmployeeBean>());
        return em;
    }

    @RequestMapping("cutByItem")
    public IPage<EmployeeBean> cutByItem(Integer pageNO, String name, String dept) {
        IPage<EmployeeBean> ip = service.cutByItem(pageNO, name, dept);
        return ip;
    }

//    @RequestMapping("findByIds")
//    public List<EmployeeBean> findByIds(Integer[] idArray) {
//        return service.findByIds(idArray);
//    }

    @RequestMapping("findByIds")
    public Collection<EmployeeBean> findByIds(Integer[] idArray, HttpServletRequest req) {
        HashMap map = (HashMap<Integer, EmployeeBean>) req.getSession().getAttribute("emList");
        List<EmployeeBean> emList = service.findByIds(idArray);
        int num = 0;
        for (Integer i : idArray) {
            map.put(i, emList.get(num));
            num++;
        }
        req.getSession().setAttribute("emList", map);
        return map.values();
    }

    @RequestMapping("del")
    public Collection<EmployeeBean> del(Integer id, HttpServletRequest req) {
        HashMap<Integer, EmployeeBean> map = (HashMap<Integer, EmployeeBean>) req.getSession().getAttribute("emList");
        map.remove(id);
        req.getSession().setAttribute("emList", map);
        return map.values();
    }


}
